﻿@model MvcCms.Store.Web.ViewModels.DefaultViewModel
@helper Truncate(string input, int length)
{    
    if (input!=null)
    {
        if (input.Length <= length)
        {
            @input
        }
        else
        {
            @input.Substring(0, length)<text>...</text>
        }
    }
}
<h2>
    Edit</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm(Html.BeginForm("edit", "storemanager", FormMethod.Post, new { autocomplete = "off" })))
{
    @Html.ValidationSummary(true)
    <div class="edit-product-container">
        <fieldset>
            <legend>Product</legend>
            @Html.HiddenFor(model => model._Product.ProductID)
            <div class="editor-label">
                @Html.LabelFor(model => model.CategoryID, "Category")
            </div>
            <div class="editor-field">
                @Html.DropDownList("_Product.CategoryID", String.Empty)
                @Html.ValidationMessageFor(model => model.CategoryID)
            </div>
            <div class="editor-label">
                @Html.LabelFor(model => model._Product.Title)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model._Product.Title)
                @Html.ValidationMessageFor(model => model._Product.Title)
            </div>
            <div class="editor-label">
                @Html.LabelFor(model => model._Product.Description)
            </div>
            <div class="editor-field">
                @Html.TextAreaFor(model => model._Product.Description, new { @class = "textarea" })
                @Html.ValidationMessageFor(model => model._Product.Description)
            </div>
            <div class="editor-label">
                @Html.LabelFor(model => model._Product.Price)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model._Product.Price)
                @Html.ValidationMessageFor(model => model._Product.Price)
            </div>
            <div class="editor-label">
                @Html.LabelFor(model => model._Product.Active)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model._Product.Active)
                @Html.ValidationMessageFor(model => model._Product.Active)
            </div>
            <div class="editor-label">
                @Html.LabelFor(model => model._Product.ProductArtUrl)
            </div>
            <div class="editor-field">
                @Html.DropDownList("_Product.ProductArtUrl", String.Empty)
                @Html.ValidationMessageFor(model => model._Product.ProductArtUrl)
            </div>
            <p>
                <input type="submit" value="Save" />
            </p>
        </fieldset>
    </div>    
    <div class="edit-product-priceOptions">
        <fieldset>
            <legend>Price Options For Product</legend>
            <table>
                <tr>
                    <th>
                        Price
                    </th>
                    <th>
                        Enabled
                    </th>
                    <th>
                        Title
                    </th>
                </tr>
                @foreach (var priceOption in Model._PriceOptions)
                {
                    var optionIsChecked = false;
                    var priceOptionExists = Model._Product.PricingOptions.Where(po => po.PriceOptionID == priceOption.PriceOptionID).FirstOrDefault();
                    if (priceOptionExists != null) { optionIsChecked = true; }
                    <tr>
                        <td>
                            @Html.DisplayFor(modelItem => priceOption.Price)
                        </td>
                        <td>
                            @Html.CheckBox(priceOption.PriceOptionID.ToString(), optionIsChecked, new { onclick="ToggelOption(this);" })
                        </td>
                        <td>                        
                            @priceOption.Title
                        </td>
                    </tr>
                }
            </table>
            <br />
            If a price option is selected the product price is over-ridden.
        </fieldset>
    </div>
}
<div style="clear:both"></div>
<div>
    @Html.ActionLink("Back to List", "Index")
</div>
<script type="text/javascript" id="BiteMyButt">
    function ToggelOption(checkBox) {
        var id = $(checkBox).attr('name');        
        var on = $(checkBox).is(':checked');

        var jdata = { 
            PriceOptionID: id,
            On : on,
            ProductID : @Model._Product.ProductID
        };

        $.ajax({
            url: "/storeManager/ToggelPriceOption",
            type: "POST",
            dataType: "text json",
            data: jdata,
            success: function (result) {
                if (!result.success){
                    alert(result.error);
                }
            }
        })
    }
</script>